//
//  HButtonView.m
//  PigMarket
//
//  Created by Felix on 2019/3/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "HButtonView.h"

@interface HButtonView ()

@property(nonatomic, strong) UIImageView *leftIcon;

@property(nonatomic, strong) UIImageView *rightIcon;

@property(nonatomic, strong) UIImageView *topIcon;

@property(nonatomic, strong) UIImageView *bottomIcon;

@property(nonatomic, strong) UILabel *titleLabel;

@property(nonatomic, strong) UIView *borderView;

@end

@implementation HButtonView

#pragma mark - init method

- (instancetype) initWithLeftIcon:(UIImageView *)leftIcon
                            title:(UILabel *)titleLabel {
    return [self initWithTopIcon:nil leftIcon:leftIcon bottomIcon:nil rightIcon:nil title:titleLabel badgeView:nil];
}

- (instancetype) initWithRightIcon:(UIImageView *)rightIcon
                             title:(UILabel *)titleLabel {
    return [self initWithTopIcon:nil leftIcon:nil bottomIcon:nil rightIcon:rightIcon title:titleLabel badgeView:nil];
}

- (instancetype) initWithTopIcon:(UIImageView *)topIcon
                           title:(UILabel *)titleLabel {
    return [self initWithTopIcon:topIcon leftIcon:nil bottomIcon:nil rightIcon:nil title:titleLabel badgeView:nil];
}
- (instancetype) initWithTopIcon:(UIImageView *)topIcon
                       badgeView:(UIView *)badgeView
                           title:(UILabel *)titleLabel {
    return [self initWithTopIcon:topIcon leftIcon:nil bottomIcon:nil rightIcon:nil title:titleLabel badgeView:badgeView];
}

- (instancetype) initWithBottomIcon:(UIImageView *)bottomIcon
                              title:(UILabel *)titleLabel {
    return [self initWithTopIcon:nil leftIcon:nil bottomIcon:bottomIcon rightIcon:nil title:titleLabel badgeView:nil];
}

- (instancetype) initWithTopIcon:(nullable UIImageView *)topIcon
                        leftIcon:(nullable UIImageView *)leftIcon
                      bottomIcon:(nullable UIImageView *)bottomIcon
                       rightIcon:(nullable UIImageView *)rightIcon
                           title:(nullable UILabel *)titleLabel
                       badgeView:(nullable UIView *)badgeView {
    
    self = [self initWithFrame:CGRectZero];
    if (self) {
        self.topIcon = topIcon;
        self.leftIcon = leftIcon;
        self.badgeView = badgeView;
        self.bottomIcon = bottomIcon;
        self.rightIcon = rightIcon;
        self.titleLabel = titleLabel;
        [self initView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self initLayout];
}


- (void)initView {
    // 添加子控件
    [self addSubview:self.borderView];
    
    if (self.topIcon) {
        [self.borderView addSubview:self.topIcon];
    }
    
    if (self.leftIcon) {
        [self.borderView addSubview:self.leftIcon];
    }
    
    if (self.bottomIcon) {
        [self.borderView addSubview:self.bottomIcon];
    }
    
    if (self.rightIcon) {
        [self.borderView addSubview:self.rightIcon];
    }
    if (self.badgeView) {
        [self.borderView addSubview:self.badgeView];
    }
    [self.borderView addSubview:self.titleLabel];
    
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)];
    [self addGestureRecognizer:tapGesture];
}

- (void)viewTapped {
    if (_tapped) {
        _tapped();
    }
}

- (void)initLayout {
    
    UIView *superView = self.borderView;
    
    [self.borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
    }];
    
    if (self.topIcon) {
        [self.topIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.centerX.mas_equalTo(superView);
        }];
        if (self.badgeView) {
            [self.badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.topIcon.mas_right).offset(-4);
                make.centerY.mas_equalTo(self.topIcon.mas_top);
//                make.size.mas_equalTo(CGSizeMake(10, 10));
            }];
        }
    }
    
    if (self.bottomIcon) {
        [self.bottomIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
            make.centerX.mas_equalTo(superView);
        }];
    }
    
    if (self.leftIcon) {
        [self.leftIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(0);
            make.centerY.mas_equalTo(superView);
        }];
    }
    
    if (self.rightIcon) {
        [self.rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(0);
            make.centerY.mas_equalTo(superView);
        }];
    }
    
    int padding = 8;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        self.topIcon ? make.top.mas_equalTo(self.topIcon.mas_bottom).offset(padding) : make.top.mas_equalTo(0);
        self.bottomIcon ? make.bottom.mas_equalTo(self.bottomIcon.mas_top).offset(-padding)  : make.bottom.mas_equalTo(0);
        self.leftIcon ? make.leading.mas_equalTo(self.leftIcon.mas_trailing).offset(padding)  : make.leading.mas_equalTo(0);
        self.rightIcon ? make.trailing.mas_equalTo(self.rightIcon.mas_leading).offset(-padding)  : make.trailing.mas_equalTo(0);
    }];
    
}

-(CGSize)intrinsicContentSize {
    
    CGSize titleSize = [self.titleLabel intrinsicContentSize];
    
    CGSize topSize = CGSizeMake(0, 0);
    if(self.topIcon) {
        topSize = [self.topIcon intrinsicContentSize];
    }
    
    CGSize leftSize = CGSizeMake(0, 0);
    if(self.leftIcon) {
        leftSize = [self.leftIcon intrinsicContentSize];
    }
    
    CGSize bottomSize = CGSizeMake(0, 0);
    if(self.bottomIcon) {
        bottomSize = [self.bottomIcon intrinsicContentSize];
    }
    
    CGSize rightSize = CGSizeMake(0, 0);
    if(self.rightIcon) {
        rightSize = [self.rightIcon intrinsicContentSize];
    }
    
    CGFloat width = leftSize.width + rightSize.width + fmax(fmax(topSize.width, bottomSize.width), titleSize.width);
    CGFloat height =  topSize.height + bottomSize.height + fmax(fmax(leftSize.height, rightSize.height), titleSize.height);
    
    return CGSizeMake(width, height);
}

- (UIView *)borderView {
    
    if(!_borderView) {
        _borderView = [[UIView alloc] init];
    }
    
    return _borderView;
}


@end
